package com.google.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;

public class IGooglePage extends Page{
	
	@FindBy(className = "G-DN")
	public java.util.List<WebElement> widgetList;
	
	@FindBy(id = "home_dd8")
	public WebElement bookmarkWidgetCustomizationButton;
	
	@FindBy(id = "gdd_delete")
	public WebElement deleteWidgetMenuLink;
	
	@FindBy(id = "gdd_minimize")
	public WebElement minimizeWidgetMenuLink;
	
	@FindBy(id = "gdd_expand")
	public WebElement expandWidgetMenuLink;
	
	@FindBy(id = "gdd_maximize")
	public WebElement maximizeWidgetMenuLink;
	
	@FindBy(xpath = "//button[text()='OK']")
	public WebElement confirmButton;
	
	public IGooglePage (WebDriver driver) {
		super (driver);
	}

	public void open() {
		driver.get("http://www.google.com/ig?source=gama");
	}
	
	/*public void bookmarkWidgetCustomizationButtonClick() {
		try {
			Actions action = new Actions(driver);
			action.moveToElement(bookmarkWidgetCustomizationButton).perform();
			bookmarkWidgetCustomizationButton.click();
		} catch (ElementNotVisibleException e) {
			e.printStackTrace();
		}
	}*/
	
	public void widgetCustomizationButtonClick(int n) {
		try {
			Actions action = new Actions(driver);
			WebElement widget = widgetList.get(n);
			System.out.println(widget.getText());
			action.moveToElement(widget).perform();
			widget.findElement(By.className("G-HN")).click();
		} catch (ElementNotVisibleException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteWidget() {
		deleteWidgetMenuLink.click();
		confirmButton.click();
	}
	
	public void minimizeWidget(int n) {
		//System.out.println(widgetList.get(n-1).findElement(By.className("G-HN")).isDisplayed());
		if (!minimizeWidgetMenuLink.isDisplayed()) {
			widgetCustomizationButtonClick(n);
		}
		minimizeWidgetMenuLink.click();
	}
	
	public void expandWidget(int n) {
		
		if (!expandWidgetMenuLink.isDisplayed()) {
			widgetCustomizationButtonClick(n);
		}
		expandWidgetMenuLink.click();
	}

}
